package com.ddwl.user.dao;

import cn.hutool.core.util.StrUtil;
import com.ddwl.user.dao.mapper.UserAuthMapper;
import com.ddwl.user.dao.model.UserAuth;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

/**
 * 类UserAuthDao.java的实现描述：用户微信认证表
 *
 * @author zach
 * @Date :2020-08-21 09:54.
 */
@Repository
public class UserAuthDao {

    @Resource
    private UserAuthMapper userAuthMapper;

    public UserAuth getByUserId(String userId, String product){
        UserAuth userAuth = new UserAuth();
        userAuth.setUserId(userId);
        userAuth.setProduct(product);
        return userAuthMapper.selectOne(userAuth);
    }

    public void mergeAuth(String fromUserId, String toUserId){
        userAuthMapper.mergeAuth(fromUserId, toUserId);
    }

    public UserAuth getByOpenId(String openId){
        return userAuthMapper.selectByPrimaryKey(openId);
    }

    public void add(UserAuth userAuth){
        userAuthMapper.insert(userAuth);
    }

    public void deleteByUserId(String userId, String product) {
        userAuthMapper.deleteByUserId(userId, product);
    }

    public void updateByUserIdAndPk(UserAuth entity,String pk) {
        if (null != entity && StrUtil.isNotEmpty(entity.getUserId())) {
            Example example = new Example(UserAuth.class);
            example.and().andEqualTo("userId", entity.getUserId())
                    .andEqualTo("product", pk);
            userAuthMapper.updateByExampleSelective(entity,example);
        }
    }

    public void updateOtherAuth(UserAuth userAuth, String product) {
        if (null != userAuth && StrUtil.isNotEmpty(userAuth.getUserId())) {
            Example example = new Example(UserAuth.class);
            example.and().andEqualTo("userId", userAuth.getUserId())
                    .andNotEqualTo("product", product);
            userAuthMapper.updateByExampleSelective(userAuth,example);
        }
    }

}
